home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / applet / communication / example / Sender.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.8 KB  |  108 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.*;
  18. import java.awt.*;
  19. import java.util.Enumeration;
  20.  
  21. public class Sender extends Applet {
  22.     private String myName;
  23.     private TextField nameField;
  24.     private TextArea status;
  25.  
  26.     public void init() {
  27.         GridBagLayout gridBag = new GridBagLayout();
  28.         GridBagConstraints c = new GridBagConstraints();
  29.  
  30.         setLayout(gridBag);
  31.  
  32.         Label receiverLabel = new Label("Receiver name:", Label.RIGHT);
  33.         gridBag.setConstraints(receiverLabel, c);
  34.         add(receiverLabel);
  35.  
  36.         nameField = new TextField(getParameter("RECEIVERNAME"), 10);
  37.         c.fill = GridBagConstraints.HORIZONTAL;
  38.         gridBag.setConstraints(nameField, c);
  39.         add(nameField);
  40.  
  41.         Button button = new Button("Send message");
  42.         c.gridwidth = GridBagConstraints.REMAINDER; //end row
  43.         c.anchor = GridBagConstraints.WEST; //stick to the text field
  44.         c.fill = GridBagConstraints.NONE; //keep the button small
  45.         gridBag.setConstraints(button, c);
  46.         add(button);
  47.  
  48.         status = new TextArea(5, 60);
  49.         status.setEditable(false);
  50.         c.anchor = GridBagConstraints.CENTER; //reset to the default
  51.         c.fill = GridBagConstraints.BOTH; //make this big
  52.         c.weightx = 1.0;
  53.         c.weighty = 1.0;
  54.         gridBag.setConstraints(status, c);
  55.         add(status);
  56.  
  57.         myName = getParameter("NAME");
  58.         Label senderLabel = new Label("(My name is " + myName + ".)", 
  59.                                 Label.CENTER);
  60.         c.weightx = 0.0;
  61.         c.weighty = 0.0;
  62.         gridBag.setConstraints(senderLabel, c);
  63.         add(senderLabel);
  64.  
  65.         validate();
  66.     }
  67.  
  68.     public boolean action(Event event, Object o) {
  69.         Applet receiver = null;
  70.         String receiverName = nameField.getText(); //Get name to search for.
  71.         receiver = getAppletContext().getApplet(receiverName);
  72.         if (receiver != null) {
  73.         //Use the instanceof operator to make sure the applet
  74.         //we found is a Receiver object.
  75.             if (!(receiver instanceof Receiver)) {
  76.                 status.appendText("Found applet named "
  77.                                   + receiverName + ", "
  78.                                   + "but it's not a Receiver object.\n");
  79.             } else {
  80.                 status.appendText("Found applet named "
  81.                                   + receiverName + ".\n"
  82.                                   + "  Sending message to it.\n");
  83.         //Cast the receiver to be a Receiver object
  84.         //(instead of just an Applet object) so that the
  85.         //compiler will let us call a Receiver method.
  86.                 ((Receiver)receiver).processRequestFrom(myName);
  87.             }
  88.         } else {
  89.             status.appendText("Couldn't find any applet named "
  90.                               + receiverName + ".\n");
  91.         }
  92.         return false;
  93.  
  94.     }
  95.  
  96.     public Insets insets() {
  97.         return new Insets(3,3,3,3);
  98.     }
  99.  
  100.     public void paint(Graphics g) {
  101.         g.drawRect(0, 0, size().width - 1, size().height - 1);
  102.     }
  103.  
  104.     public String getAppletInfo() {
  105.         return "Sender by Kathy Walrath";
  106.     }
  107. }
  108.